transaction.
Figure 2 transaction class diagram
1. Concurrency Management
Each transaction has its own concurrency Manager. The concurrency manager tracks the locks held by the current transaction and exchanges information with the global lock table when necessary. It can be seen that in the concurrent management of simpledb, the core is the lock table. Through the operation of the lock table data, the concurrent management is realized.
> Locktable
The lock table provides an interf
the database execution policies and methods, such as the first execution and the second execution of the database execution policy will be different). However, because T1 has added an exclusive lock for a record, causing T2 's full table scan to continue, it causes T2 to wait. How does a deadlock work? One approach is as follows: Example 6:----------------------------------------t1:begin transelect * from table (Xlock) (
id=20, need to scan the full table, then the table will be pre-shared lock or update lock or exclusive lock (dependent on the database execution policies and methods, such as the first execution and the second execution of the database execution policy will be different).
However, because T1 has added an exclusive lock for a record, causing T2 's full table scan to continue, it causes T2 to wait. How does a deadlock work? One approach is as follows: Example 6:----------------------------------
update lock or exclusive lock (dependent on the database execution policies and methods, such as the first execution and the second execution of the database execution policy will be different). However, because T1 has added an exclusive lock for a record, causing T2 's full table scan to continue, it causes T2 to wait. How does a deadlock work? One approach is as follows: Example 6:----------------------------------------t1:begin transelect * from table (X
is too large, so the implementation of the Linux kernel protocol stack adopts a more elegant method.
Protocol Stack lock socket: Due to Soft Interrupt Processing protocol stack, it may run in any context after the hard interrupt, so it cannot sleep, so it must be a spin lock slock, which is maintained by the socket itself, this lock not only protects the race state between user processes, but also protects the race state between operations on the same socket protocol stack on different CPUs (ve
update lock or exclusive lock (dependent on the database execution policies and methods, such as the first execution and the second execution of the database execution policy will be different). However, because T1 has added an exclusive lock for a record, causing T2 's full table scan to continue, it causes T2 to wait. How does a deadlock work? One approach is as follows: Example 6:----------------------------------------t1:begin transelect * from table (X
update lock or exclusive lock (dependent on the database execution policies and methods, such as the first execution and the second execution of the database execution policy will be different). However, because T1 has added an exclusive lock for a record, causing T2 's full table scan to continue, it causes T2 to wait. How does a deadlock work? One approach is as follows: Example 6:----------------------------------------t1:begin transelect * from table (X
update lock or exclusive lock (dependent on the database execution policies and methods, such as the first execution and the second execution of the database execution policy will be different). However, because T1 has added an exclusive lock for a record, causing T2 's full table scan to continue, it causes T2 to wait. How does a deadlock work? One approach is as follows: Example 6:----------------------------------------t1:begin transelect * from table (X
update lock or exclusive lock (dependent on the database execution policies and methods, such as the first execution and the second execution of the database execution policy will be different). However, because T1 has added an exclusive lock for a record, causing T2 's full table scan to continue, it causes T2 to wait. How does a deadlock work? One approach is as follows: Example 6:----------------------------------------t1:begin transelect * from table (X
in order to find id=20, need to scan the full table, then the table will be pre-shared lock or update lock or exclusive lock (dependent on the database execution policies and methods, such as the first execution and the second execution of the database execution policy will be different).
However, because T1 has added an exclusive lock for a record, causing T2 's full table scan to continue, it causes T2 to wait. How does a deadlock work? One approach is as follows: Example 6:-----------------
=20, need to scan the full table, then the table will be pre-shared lock or update lock or exclusive lock (dependent on the database execution policies and methods, such as the first execution and the second execution of the database execution policy will be different).
However, because T1 has added an exclusive lock for a record, causing T2 's full table scan to continue, it causes T2 to wait. How does a deadlock work? One approach is as follows: Example 6:-------------------------------------
-shared lock or update lock or exclusive lock (dependent on the database execution policies and methods, such as the first execution and the second execution of the database execution policy will be different).
However, because T1 has added an exclusive lock for a record, causing T2 's full table scan to continue, it causes T2 to wait. How does a deadlock work? One approach is as follows: Example 6:----------------------------------------t1:begin Tran SELECT * FROM table (
in order to find id=20, need to scan the full table, then the table will be pre-shared lock or update lock or exclusive lock (dependent on the database execution policies and methods, such as the first execution and the second execution of the database execution policy will be different). However, because T1 has added an exclusive lock for a record, causing T2 's full table scan to continue, it causes T2 to wait. How does a deadlock work? One approach is as follows: Example 6:------------------
the past that SQL server adopts optimistic locks: For update, insert, delete uses the X lock automatically. For select, the S lock is automatically used. For pessimistic locks, the programmer needs to control the locking and unlocking actions.
Let's take a look at how to use the lock to solve the previous conflict:
T1T2T1T2T1T2
①Xlock
Read a = 10
Slock
Slock B
Read a = 50
Read B = 100
A + B = 150
Xlock
Rea
affected)Since the case comes from the system renewal issue, the business is implemented by invoking the stored procedure, so each call is a select+x lock, which is not the same as the "select+x Lock and Select+s lock" mentioned in the above article.First of all, my mistake.MYTH: The x Lock specified in select will be released immediately after the query ends and does not last until Tran endsThe test code is as follows:--session_aBEGIN TRANSELECT * from [test_a].[dbo].[tmp_byxl_01](
be added to an exclusive lock 2. Exclusive Lock (Xlock) An exclusive lock can be simply understood as a conditional update or query if you force an exclusive lock on a table you can use the Handwriting keyword (xlock) to include: SELECT * from #t1 (xlock) This forces the resource to be given a single lock, and the advantage is that we are doing some updated SQL
[dbo]. Sale@ Name nvarchar (50 ),@ Number int,@ Result bit outputAsBegin tranDeclare @ Quantity intSelect @ Quantity = Quantity from Stock with (ROWLOCK, XLOCK) where Name = @ nameIf @ Quantity >=@ number beginWaitfor delay '00: 00: 10' -- todoUpdate Stock set Quantity = Quantity-@ number where Name = @ nameEndIf @ rowcount> 0 beginSelect @ result = 1CommitEndElse beginSelect @ result = 0RollbackEndGo Additional instructions[1]. When no lock
we make judgments to prevent problems caused by similar concurrency?
So I thought about the lock. In fact, when I thought about the lock, I was not very familiar with it. I never understood the explicit lock prompts. it would be okay if there were any problems. So let's test it.
So I changed the stored procedure to this
Alter proc ups_TestLock @ I intasbeginbegin trybegin tranif not exists (select 1 from t with (xlock, rowlock) where id = @ I) -- Not
obviously a bit more complicated. The classification of locks, in the textbook, the network is a lot of two dimensions to describe. A dimension is divided by the function of lock, and one dimension is divided by concept. 09, I did a database training tutorial, the lock classification to cut out the pendulum. After a few years, it seems that PPT looks very rough. I can't compare these ppt templates, but the content is still classic. Three: Key words of the lock Shared locks, locks such as loc
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.